﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using UserServiceLibrary;
using UserClassLIB;

namespace NascarEventsWPF
{
    /// <summary>
    /// Interaction logic for Page1_Users.xaml
    /// </summary>
    public partial class Page1_Users : Window
    {
        public Page1_Users()
        {
            InitializeComponent();
        }

        private void btnBack_Click(object sender, RoutedEventArgs e)
        {
            MainWindow mw = new MainWindow();
            mw.ShowDialog();
        }

        private UserServiceLibrary.UserService UserService = new UserServiceLibrary.UserService();

        private void CB_User_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string SelectedUserID = CB_User.SelectedValue.ToString();

            Object selectedItem = CB_User.SelectedItem;
           
            UserClassLIB.UserClass aUser = (UserClassLIB.UserClass)selectedItem;

            Tb_FName.Text = aUser.FirstName;
            Tb_EmpID.Text = aUser.EmployeeID.ToString();
            Tb_LName.Text = aUser.LastName;
            Tb_UserName.Text = aUser.Username;
            Tb_Password.Text = aUser.Password;
            Tb_userID.Text = aUser.UserID.ToString();
        }

        private void Window(object sender, RoutedEventArgs e)
        {
            try
            {
                CB_User.ItemsSource = UserService.GetAllUser();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Btn_Update_Click(object sender, RoutedEventArgs e)
        {
            UserClass aUser = new UserClass();
            
            aUser.FirstName = Tb_FName.Text;
            aUser.EmployeeID = Convert.ToInt32(Tb_EmpID.Text);
            aUser.LastName = Tb_LName.Text;
            aUser.Username = Tb_UserName.Text;
            aUser.Password = Tb_Password.Text;
            aUser.UserID = Convert.ToInt32(Tb_userID.Text);

            bool isUpdated = UserService.UpdateUser(aUser);

            if (isUpdated)
            {
                MessageBox.Show("Your information is successfully updated!");
            }
        }

        private void Btn_Add_Click(object sender, RoutedEventArgs e)
        {
            UserClass aUser = new UserClass();

            aUser.FirstName = Tb_FName.Text;
            aUser.EmployeeID = Convert.ToInt32(Tb_EmpID.Text);
            aUser.LastName = Tb_LName.Text;
            aUser.Username = Tb_UserName.Text;
            aUser.Password = Tb_Password.Text;
            aUser.UserID = Convert.ToInt32(Tb_userID.Text);

            bool isAdded = UserService.AddUser(aUser);

            if (isAdded)
            {
                MessageBox.Show("Your information is successfully added!");
            }
        }

        private void Btn_Delete_Click(object sender, RoutedEventArgs e)
        {
            string SelectedUserID = CB_User.SelectedValue.ToString();

            Object selectedItem = CB_User.SelectedItem;

            UserClassLIB.UserClass aUser = (UserClassLIB.UserClass)selectedItem;
         
            Tb_FName.Text = aUser.FirstName;
            Tb_EmpID.Text = aUser.EmployeeID.ToString();
            Tb_LName.Text = aUser.LastName;
            Tb_UserName.Text = aUser.Username;
            Tb_Password.Text = aUser.Password;
            Tb_userID.Text = aUser.UserID.ToString();

            bool isDeleted = UserService.DeleteUser(aUser);

            if (isDeleted)
            {
                MessageBox.Show("Your information is successfully deleted!");
            }
        }

        private void Btn_Clear_Click(object sender, RoutedEventArgs e)
        {
            Tb_userID.Clear();
            Tb_FName.Clear();
            Tb_LName.Clear();
            Tb_EmpID.Clear();
            Tb_UserName.Clear();
            Tb_Password.Clear();
        }

        private void Btn_Exit_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }
    }
}
